package com.xyq;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
  private static ArrayList<Student> list = new ArrayList<>();

  private static Scanner sc = new Scanner(System.in);

  public static void main(String[] args) {
    while (true) {
      System.out.println("--------学生管理系统--------");
      System.out.println("1. 添加学生");
      System.out.println("2. 查看所有学生");
      System.out.println("3. 删除学生");
      System.out.println("4. 修改学生");
      System.out.println("5. 退出");
      System.out.print("请输入你的选择：");

      int  choice =-1;
      try {
        choice = Integer.parseInt(sc.nextLine());
      } catch (NumberFormatException e) {
        System.out.println("输入无效，请输入数字！");
        continue;
      }

      switch (choice) {
        case 1 -> addStudent();
        case 2 -> showAllStudents();
        case 3 -> deleteStudent();
        case 4 -> updateStudent();
        case 5 -> {
          System.out.println("退出系统，再见！");
          return;
        }
        default -> System.out.println("选择错误，请重新输入！");
      }
    }
  }

  // 添加学生
  private static void addStudent() {
    System.out.println("请输入学生信息：");
    System.out.print("学号：");
    String id = sc.nextLine();

    if (getIndex(id) != -1) {
      System.out.println("该学号已存在，请重新输入！");
      return;
    }

    System.out.print("姓名：");
    String name = sc.nextLine();
    System.out.print("地址：");
    String address = sc.nextLine();

    System.out.print("年龄：");
    int age = 0;
    boolean rightAge = false;
    while (!rightAge) {
      try {
        age = Integer.parseInt(sc.nextLine());
        rightAge = true; // 输入正确，跳出循环
      } catch (NumberFormatException e) {
        System.out.print("年龄格式错误，请重新输入：");
      }
    }


    list.add(new Student(id, name, age,address));
    System.out.println("添加成功！");
  }

  // 显示所有学生
  private static void showAllStudents() {
    if (list.isEmpty()) {
      System.out.println("暂无学生信息！");
      return;
    }

    System.out.println("学号\t姓名\t年龄");
    for (Student s : list) {
      System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "岁");
    }
  }

  // 删除学生
  private static void deleteStudent() {
    System.out.print("请输入要删除的学生学号：");
    String id = sc.nextLine();

    int index = getIndex(id);
    if (index == -1) {
      System.out.println("该学号不存在！");
      return;
    }

    list.remove(index);
    System.out.println("删除成功！");
  }

  // 修改学生
  private static void updateStudent() {
    System.out.print("请输入要修改的学生学号：");
    String id = sc.nextLine();

    int index = getIndex(id);
    if (index == -1) {
      System.out.println("该学号不存在！");
      return;
    }

    Student s = list.get(index);

    System.out.print("新的姓名（当前：" + s.getName() + "）：");
    String name = sc.nextLine();
    if (!name.isEmpty()) {
      s.setName(name);
    }

    System.out.print("新的年龄（当前：" + s.getAge() + "）：");
    String ageStr = sc.nextLine();
    if (!ageStr.isEmpty()) {
      try {
        s.setAge(Integer.parseInt(ageStr));
      } catch (NumberFormatException e) {
        System.out.println("年龄格式错误，未修改");
      }
    }

    System.out.println("修改成功！");
  }

  // 获取学号
  private static int getIndex(String id) {
    for (int i = 0; i < list.size(); i++) {
      if (list.get(i).getId().equals(id)) {
        return i;
      }
    }
    return -1;
  }
}
